<?php

class Eleven_Resource_Announcement extends CMS_Model_Resource_Db_Table_Abstract
{
	protected $_name = 'announcement';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Announcement_Item';
	
	public function getAnnouncements($paged = false, $order = null) {
		$select = $this->select ();
		$select->from ( 'announcement' );
		
		if (true === is_array ( $order )) {
		    $order = array_merge(array('top DESC'), $order);
			$select->order ( $order );
		}
		
		if (null !== $paged) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'announcement', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 12 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
		return $this->fetchAll ( $select );
	}
	
	public function getIndexAnnouns($total=7)
	{
	    $select = $this->select();
	    $order = array('top DESC', 'publishdate DESC');
	    $select->order($order);
	    $select->limit($total);
	    return $this->fetchAll($select);    
	}
	public function getAnnounceById($id)
	{
		return $this->find ( $id )->current ();
	}
	
	public function deleteAnnounceById($id)
	{
	    return $this->delete('id = '.$id);
	}
	
	public function setTops($tops, $isCancel=false)
	{
	    if (is_array($tops))
        {
            $where = $this->getAdapter()->quoteInto('id IN (?)', $tops);
            if ($isCancel)
            {
                $data = array(
                	'top'	=> 0,
                );   
            }else{
                $data = array(
                	'top'	=> 1,
                );
            }
            
            $this->update($data, $where);
        }
	}
}

?>